﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Drawing;
using HZ.Supervise.Model;
using System.Web.UI.DataVisualization.Charting;
namespace HZ.Supervise.Web.Secondphase.ReportCompany
{
    public partial class ReportOfCompanyFirstSc : BasePage
    {
        System.Data.DataTable dt;
        string order = "SortName asc";
        protected string DrugsTotal, FactoryTotal, DistributionTotal;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                Databind();
                GetStatistics();
                BindChart();
            }
        }

        /// <summary>
        /// 图表绑定 
        /// </summary>
        private void BindChart()
        {

            //空为初始页
            if (dt.Rows.Count > 0)
            {
                string monch = "";//月份
                int count = 2;//次数
                string sort = "0";//属性 0:基药 1:非基药

                #region 左图

                //***************** 基本供货采购数量*****************/
                Series series1 = new Series("基本药品供货采购数量");
                series1.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series1.BorderWidth = 1;
                series1.ShadowOffset = 1;


                //***************** 非基供货采购数量*******************/
                Series series2 = new Series("非基本药品供货采购数量");
                series2.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series2.BorderWidth = 1;
                series2.ShadowOffset = 1;

                //***********************基本药品供货采购数量******************/
                Series series3 = new Series("基本药品供货配送数量");
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series3.BorderWidth = 1;
                series3.ShadowOffset = 1;

                //***********************非基本药品供货采购数量******************/
                Series series4 = new Series("非基本药品供货配送数量");
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series4.BorderWidth = 1;
                series4.ShadowOffset = 1;
                #endregion

                #region 绑定值
                if (dt.Rows[0]["Source"].ToString().Equals("1"))
                {
                    count = 1;
                    monch = dt.Rows[0]["addtime"].ToString();
                }
                for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                {
                    if (dt.Rows[i]["addtime"].ToString().Equals(monch))
                    {
                        if (i == 0)
                        {
                            series1.Points.AddXY(monch.Substring(4) + "月", 0);
                            series3.Points.AddXY(monch.Substring(4) + "月", 0);

                        }
                        series2.Points.AddXY(dt.Rows[i]["addtime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCount"].ToString())).ToString());
                        series4.Points.AddXY(dt.Rows[i]["addtime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["DistributeCount"].ToString())).ToString());
                        count++;
                        sort = "1";

                    }
                    else
                    {
                        if (count == 2 && dt.Rows[i]["Source"].ToString().Equals("0"))
                        {
                            series1.Points.AddXY(dt.Rows[i]["addtime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCount"].ToString())).ToString());
                            series3.Points.AddXY(dt.Rows[i]["addtime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["DistributeCount"].ToString())).ToString());
                            count = 1;
                            sort = "0";
                            monch = dt.Rows[i]["addtime"].ToString();
                        }
                        else
                        {
                            if (sort.Equals("0"))
                            {
                                series2.Points.AddXY(monch.Substring(4) + "月", 0);
                                series4.Points.AddXY(monch.Substring(4) + "月", 0);
                            }
                            else
                            {
                                series1.Points.AddXY(monch.Substring(4) + "月", 0);
                                series3.Points.AddXY(monch.Substring(4) + "月", 0);

                            }

                            if (dt.Rows[i]["Source"].ToString().Equals("0"))
                            {
                                series1.Points.AddXY(dt.Rows[i]["addtime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCount"].ToString())).ToString());
                                series3.Points.AddXY(dt.Rows[i]["addtime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["DistributeCount"].ToString())).ToString());
                                monch = dt.Rows[i]["addtime"].ToString();
                                count = 1;
                                sort = "0";
                            }
                            else
                            {
                                series2.Points.AddXY(dt.Rows[i]["addtime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PuchaseCount"].ToString())).ToString());
                                series4.Points.AddXY(dt.Rows[i]["addtime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["DistributeCount"].ToString())).ToString());
                                count = 2;
                                sort = "1";
                            }

                        }

                    }
                }
                #endregion

                #region 数据绑定到图
                //左图
                series1.IsValueShownAsLabel = false;
                ChartSupply.Series.Add(series1);
                ChartSupply.Series["基本药品供货采购数量"].ChartArea = "ChartSupplyColumn";

                series2.IsValueShownAsLabel = false;
                ChartSupply.Series.Add(series2);
                ChartSupply.Series["非基本药品供货采购数量"].ChartArea = "ChartSupplyColumn";

                series3.IsValueShownAsLabel = false;
                ChartSupply.Series.Add(series3);
                ChartSupply.Series["基本药品供货配送数量"].ChartArea = "ChartSupplyColumn";

                series4.IsValueShownAsLabel = false;
                ChartSupply.Series.Add(series4);
                ChartSupply.Series["非基本药品供货配送数量"].ChartArea = "ChartSupplyColumn";
                #endregion


                #region 两张图片属性

                ChartSupply.ChartAreas["ChartSupplyColumn"].AxisX.IsMarginVisible = true;
                ChartSupply.ChartAreas["ChartSupplyColumn"].Area3DStyle.Enable3D = false;
                ChartSupply.ChartAreas["ChartSupplyColumn"].AxisX.MajorGrid.Interval = 1;

                ChartSupply.ChartAreas["ChartSupplyColumn"].AxisX.LineWidth = 1;
                ChartSupply.ChartAreas["ChartSupplyColumn"].AxisY.LineWidth = 1;
                ChartSupply.ChartAreas["ChartSupplyColumn"].AxisY.Title = "数量";
                ChartSupply.ChartAreas["ChartSupplyColumn"].AxisX.Title = "月份";
                #endregion

                #region 绑定数据TB
                HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

                DataTable dtChart = ie.GetSuperviseCompanySC_Chart();

                if (dtChart.Rows.Count > 0)
                {
                    if (dtChart.Rows[0]["ProcureCatalogIdTotal"] != DBNull.Value) { DrugsTotal = string.Format("{0}个", dtChart.Rows[0]["ProcureCatalogIdTotal"].ToString()); }
                    if (dtChart.Rows[0]["SellnameTotal"] != DBNull.Value) { FactoryTotal = string.Format("{0}个", dtChart.Rows[0]["SellnameTotal"].ToString()); }
                    if (dtChart.Rows[0]["BuyNameTotal"] != DBNull.Value) { DistributionTotal = string.Format("{0}个", dtChart.Rows[0]["BuyNameTotal"].ToString()); }

                }

                #endregion

                #region 分组
                ChartSupply.Series["基本药品供货采购数量"]["StackedGroupName"] = "Group1";
                ChartSupply.Series["非基本药品供货采购数量"]["StackedGroupName"] = "Group1";
                ChartSupply.Series["基本药品供货配送数量"]["StackedGroupName"] = "Group2";
                ChartSupply.Series["非基本药品供货配送数量"]["StackedGroupName"] = "Group2";
                ChartSupply.ResetAutoValues();
                #endregion
            }
            else
            {
                this.Panel1.Visible = false;
                lblnull.Visible = true;
            }

        }

        void GetStatistics()
        {
            double purchaseAmountTotal = 0;//供货总金额
            double purchaseCountTotal = 0;
            for (int i = 0; i < gvwProcureCatalog.Rows.Count; i++)
            {
                purchaseAmountTotal += double.Parse(gvwProcureCatalog.Rows[i].Cells[2].Text);
                purchaseCountTotal += double.Parse(gvwProcureCatalog.Rows[i].Cells[3].Text);
            }
            this.lblCount.Text = "供货采购数量为：" + purchaseAmountTotal.ToString() + "供货配送数量为：" + purchaseCountTotal.ToString();
        }
        public void Databind()
        {
            int t = 0;
            HZ.Supervise.IBLL.IBll_CompanyOrderDetail ibcd = HZ.Supervise.BLLFactory.IBll_CompanyOrderDetail();
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
            //order += "companyName_ps='" + sq.CompanyName_PS + "' and SortName asc";
            dt = ie.FixAllMonth(ibcd.GetCompanySC_sort(1, "and companyName_ps='" + sq.CompanyName_PS + "'", order, 0, 1, out t), 2, 0, "addtime");
            gvwProcureCatalog.DataSource = dt;
            gvwProcureCatalog.DataBind();
            for (int j = 0; j < gvwProcureCatalog.Rows.Count; j++)
            {
                //名称相同才能合并
                //防止超出索引
                if (j < (gvwProcureCatalog.Rows.Count - 1))
                {
                    if (gvwProcureCatalog.Rows[j].Cells[0].Text.Trim() == gvwProcureCatalog.Rows[j + 1].Cells[0].Text.Trim())
                    {
                        //合并头
                        gvwProcureCatalog.Rows[j].Cells[0].RowSpan = 2;
                        gvwProcureCatalog.Rows[j + 1].Cells[0].Visible = false;

                        //合并尾
                        gvwProcureCatalog.Rows[j].Cells[4].RowSpan = 2;
                        gvwProcureCatalog.Rows[j + 1].Cells[4].Visible = false;
                        j++;
                    }
                    else
                    {

                    }
                }

                //合并

            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {

        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {

        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {

        }
        protected void lbtnSee_Click(object sender, EventArgs e)
        {

        }

        protected void gvwProcureCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CompanyOfMonthSC")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = new SuperviseQuery();

                query.Mid = e.CommandArgument.ToString();

                Session[sessionid] = query;

                if (!string.IsNullOrEmpty(query.Mid))
                {
                    Response.Redirect(string.Format("CompanyOfMonthSC.aspx?sessionid={0}", sessionid));
                }
                else
                {

                }
            }
        }
    }
}
